<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="query" clearable @clear="search(query)">
              <el-button slot="append" icon="el-icon-search" @click="search(query)"></el-button>
            </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格数据 -->
      <el-table :data="userList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="密码" prop="password"></el-table-column>
        <el-table-column label="状态" prop="JSON.parse(bool)">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.stutic" @change="userStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUser(scope.row.id)"></el-button>
            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[6, 12, 18, 24]"
          :page-size="6"
          layout="sizes, prev, pager, next"
          :total="24">
        </el-pagination>
    </el-card>
    <!-- 添加用户模态框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <!-- form表单 输入框 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
         <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClosed">
      <!-- form表单 输入框 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username" >
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
         <el-form-item label="手机号" prop="mobile">
         <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getIndex} from '@/network/request.js'
export default {
  data() {
    //手机号验证规则
    var checkMobile = (rule,value,callback)=>{
      const regMobile=/^1[3-9][0-9]{9}$/;
      if(regMobile.test(value)){
        return callback()
      }
      callback(new Error('请输入正确的手机号!'))
    }
    //邮箱号的验证规则
    var checkEmail = (rule,value,callback) =>{
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if(regEmail.test(value)){
        return callback()
      }
      callback(new Error('请输入合法的邮箱!'))
    }
    return {
      userList:[
        
      ],
      query:"",
      addDialogVisible:false,
      editDialogVisible:false,
      addFormRules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        mobile:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      },
      editFormRules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        mobile:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      },
      addForm:{
          username:"",
          password:'',
          mobile:''
      },
      editForm:{
          username:"",
          password:'',
          mobile:''
      }
    }
  },
  created() {
    //组件创建后就请求数据在进行数据渲染
  },
  methods: {
    handleSizeChange(newSize){
      console.log(newSize)
    },
    handleCurrentChange(newPage){
      console.log(newPage)
    },
    userStateChange(userIfo){
      console.log(userIfo)
      this.$message.success("修改用户状态成功!")
    },
    //搜索
    search(query){
      const newList = []
      this.userList.filter((item,index)=>{
          if(item.username.includes(query)){
              newList.push(item)
          }
      })
      this.userList = newList;
    },
    //关闭清空表单
    addDialogClosed(){
      this.$refs.addFormRef.resetFields()
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields()
    },
    //添加用户
    addUser(){
      this.$refs.addFormRef.validate(valid=>{
        if(!valid) return 
        //点击确认按钮
        //将this.addForm的数据发送给后端数据库
        //发送后再次的发送请求更新数据
        //最后在this.addDialogVisible = false关闭模态框
        console.log(valid)
        console.log(this.addForm)
        //这里要请求完了数据并修改完了之后在关闭模态框  不然就没有数据发送给后台
        //this.addDialogVisible = false
      })
    },
    editUser(){
      this.$refs.editFormRef.validate(valid=>{
        if(!valid) return 
        //点击确认按钮
        //将this.editForm的数据发送给后端数据库
        //发送后再次的发送请求更新数据
        //最后在this.editDialogVisible = false关闭模态框
        console.log(valid)
        console.log(this.editForm)
        //这里要请求完了数据并修改完了之后在关闭模态框  不然就没有数据发送给后台
        //this.editDialogVisible = false;
      })
    },
    showEditDialog(id){
      this.editDialogVisible = true; 
      //拿到id发送请求  请求数据
      console.log(id)
      //这里就是手动演示一下  不是数据请求
      for(var i = 0;i<this.userList.length;i++){
        if(this.userList[i].id == id){
          console.log(this.userList[i])
          this.editForm = this.userList[i]
        }
      }
    },
    removeUser(id){
      const res = this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //在这里拿到id发送给服务器进行对应删除
        console.log(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
      console.log(res)
    }
  },
  beforeMount(){
    getIndex().then(data=>{
      this.userList = data.data
      console.log(data)
    })
  }
}
</script>

<style>

</style>